package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.TaskStock;
import com.deer.wms.base.system.model.TaskStockDto;
import com.deer.wms.base.system.model.bill.BillInDetail;
import com.deer.wms.base.system.model.bill.BillInMaster;
import com.deer.wms.base.system.model.bill.BillInMasterDto;
import com.deer.wms.base.system.model.bill.InserData;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.box.BoxItemCriteria;
import com.deer.wms.base.system.model.box.BoxItemDto;
import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.service.TaskStockService;
import com.deer.wms.base.system.service.bill.IBillInDetailService;
import com.deer.wms.base.system.service.bill.IBillInMasterService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.CommonCode;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.poi.ExcelUtil;
import com.deer.wms.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 盘点库存
 */
@Controller
@RequestMapping("/taskStock")
public class TaskStockController extends BaseController {
    private String prefix = "taskStock";

    @Autowired
    private TaskStockService taskStockService;
    @Autowired
    private IItemInfoService itemInfoService;

    @Autowired
    private IBillInMasterService billInMasterService;
    @Autowired
    private IBillInDetailService billInDetailService;
    @Autowired
    private IBoxItemService boxItemService;

    @RequiresPermissions("taskStock:view")
    @GetMapping()
    public String taskStock()
    {
        return prefix + "/taskStock";
    }


    @RequiresPermissions("taskStock:list")
    @RequestMapping("/findList")
    @ResponseBody
    public TableDataInfo findList(BoxItemCriteria criteria){
        startPage();
        List<BoxItemDto> list = boxItemService.findTaskStockList(criteria);
        return getDataTable(list);
    }

    /**
     * 导出盘点数据
     * @return
     */
    @RequiresPermissions("taskStock:export")
    @RequestMapping("/export")
    @ResponseBody
    public AjaxResult export(){
        List<BoxItemDto> list = boxItemService.findTaskExportList();
        ExcelUtil<BoxItemDto> util = new ExcelUtil<BoxItemDto>(BoxItemDto.class);
        return util.exportExcel(list, "盘点列表");
    }

    /**
     * 手持机盘点
     * @param inserData
     * @return
     */
//    @PostMapping("/numIsOK")
//    @ResponseBody
//    @Transactional
//    public Result taskStockIsOK(@RequestBody InserData inserData){
////        boolean result = false;
//        try{
//        BillInMaster billInMaster = inserData.getBillInMaster();
////        billInMaster.setCreateTime(new Date().toString());
////        billInMaster.setCreateUserId(ShiroUtils.getUserId());
////        billInMaster.setCreateUserName(ShiroUtils.getLoginName());
////        billInMaster.setState(4);
////        //更新单号数据
////        result = billInMasterService.createInfoByBillNO(billInMaster);
//        TaskStockDto taskStockDto = inserData.getTaskStockDto();
//        //比较库存与实际入库数
//        BoxItem boxItem = boxItemService.selectBoxItemByBoxCode(taskStockDto.getBoxCode());
//        BillInDetail billInDetail = billInDetailService.selectBillInDetailById(boxItem.getBillInDetailId());
//        //int num = boxItem.getQuantity() - billInDetail.getQuantityReceived();
////        TaskStock taskStock = new TaskStock();
////        taskStock.setItemCode(taskStockDto.getItemCode());
////        taskStock.setBoxCode(taskStockDto.getBoxCode());
////        taskStock.setTaskQuantity(taskStockDto.getTaskQuantity());
////        taskStock.setOffsetQuantity(num);
////        taskStock.setOperatorTime(new Date().toString());
////        //更新盘点数量
////        taskStockService.updateQuantity(taskStock);
//        if(num == 0 ){
//            return ResultGenerator.genSuccessResult(CommonCode.WS_SUCCESS,"盘点无误");
//        }else {
//            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "盘点与库存有误,误差为:"+num);
//        }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultGenerator.genFailResult(CommonCode.LACK_INFORMATION,"该物料暂无库存");
//        }
//    }
}
